/*******************************************************************************
 * Copyright (c) 2007 University of Oslo.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     University of Oslo - initial API and implementation
 *******************************************************************************/

package com.cea.papyrus.diagram.sequence.sedi.model.command;

import org.eclipse.draw2d.geometry.Rectangle;
import org.eclipse.gef.commands.Command;

import com.cea.papyrus.diagraminterchange2.di2.GraphNode;

/**
 * Command to change the position and/or size of a GraphicalElement.
 * 
 * @author Andreas Limyr
 * @author frankd@ifi.uio.no
 */
public class SetConstraintCommand extends Command {

	/** The element to be moved or resized. */
	private GraphNode model;
	private Rectangle newBounds;
	private Rectangle oldBounds;

	public SetConstraintCommand() {
	}

	public SetConstraintCommand(final GraphNode element, final Rectangle bounds) {
		if (element == null || bounds == null)
			throw new IllegalArgumentException("null");
		this.model = element;
		this.newBounds = bounds;
	}

	public void setElement(final GraphNode element) {
		this.model = element;
	}

	public void setBounds(final Rectangle bounds) {
		this.newBounds = bounds;
	}

	@Override
	public void execute() {
		oldBounds = new Rectangle(model.getPosition(), model.getSize());
		model.setPosition(newBounds.getLocation());
		model.setSize(newBounds.getSize());
	}

	@Override
	public void undo() {
		model.setPosition(oldBounds.getLocation());
		model.setSize(oldBounds.getSize());
		oldBounds = null;
	}

	@Override
	public void redo() {
		this.execute();
	}

	@Override
	public void dispose() {
		model = null;
		newBounds = null;
		oldBounds = null;
	}
}
